<template>
  <div class="template-page">
    <e-tab :tabData="templateList"
      type="card"
      @handleTab="handleTab">
      <template slot="right">
        <el-button type="default" @click="addSpace">
          <i class="el-icon-circle-plus"></i> 添加模板空间</el-button>
      </template>
    </e-tab>
    <div class="template-space">
      <el-table :data="tableData" 
        v-loading="loading"
        style="width: 100%"
        border>
        <el-table-column 
          label="序号">
          <template slot-scope="scope">
            <span>{{scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column label="名称"
          prop="spaceName">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="toDetail(scope.row)" type="text">详情</el-button>
            <el-button @click="toEdit(scope.row)" type="text">编辑</el-button>
            <el-button @click="toDel(scope.row)" type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <space-dialog 
      @addTemplateSpace="addTemplateSpace"
      @editTemplateSpace="editTemplateSpace"
      ref="spaceDialog"></space-dialog>
  </div>
</template>

<script>
import eTab from '@/components/tab';
import spaceDialog from './space-dialog';
import {findTemplates, findTemplateSpaces, insertTemplateSpace, updateTemplateSpace, deleteTemplateSpace} from '@/utils/api';
import '@/styles/template/index.scss';
export default {
  components: {eTab, spaceDialog},
  data () {
    return {
      loading: false,
      tableData: [],
      templateList: [],
      templateId: ''
    }
  },
  created () {
    findTemplates().then((res) => {
      this.templateList = res;
      this.templateId = res[0].id;
      this.getSpace();
    })
  },
  methods: {
    getSpace () {
      this.loading = true;
      findTemplateSpaces({templateId: this.templateId}).then((res) => {
        this.loading = false;
        this.tableData = res;
      }).catch((err) => {
        this.loading = false;
      })
    },
    handleTab (value) {
      this.templateId = value;
      this.getSpace();
    },
    toDetail (value) {
      this.$router.push('/template/detail/' + this.templateId + '/' + value.id)
    },
    toEdit (value) {
      this.$refs.spaceDialog.openDialog(value);
    },
    toDel (value) {
      this.$confirm('确认删除该模板吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTemplateSpace({id: value.id}).then((res) => {
          if (res) {
            this.$message.success('删除成功');
            this.getSpace();
          }
        })
      }).catch(() => {          
      });
    },
    addSpace () {
      this.$refs.spaceDialog.openDialog();
    },
    addTemplateSpace (form) {
      const params = Object.assign({}, {templateId: this.templateId}, form);
      insertTemplateSpace(params).then((res) => {
        if (res) {
          this.$message.success('添加成功')
          this.getSpace();
          this.$refs.spaceDialog.closeDialog();
        }
      })
    },
    editTemplateSpace (form) {
      const params = Object.assign({}, {templateId: this.templateId}, form);
      updateTemplateSpace(params).then((res) => {
        if (res) {
          this.$message.success('修改成功')
          this.getSpace();
          this.$refs.spaceDialog.closeDialog();
        }
      })
    }
  }
}
</script>